package com.framework.modules.job.task.yhbtask;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.framework.dao.YHBDao.user.YhbUserOrderDao;
import com.framework.entity.ter.TerTerminalInfo;
import com.framework.entity.tran.TranTransactionInfo;
import com.framework.service.yhbservice.*;
import com.framework.utils.DateUtils;
import com.framework.yhbEntity.activiy.YhbActivityInfo;
import com.framework.yhbEntity.currentcount.YhbCurrentCount;
import com.framework.yhbEntity.user.YhbUser;
import com.framework.yhbEntity.user.YhbUserOrder;
import com.framework.yhbEntity.user.YhbUserTerminalRelation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 东付解冻任务
 */
@Component("yhbDFActivTask")
@Slf4j
public class YhbDFActivTask {

    @Autowired
    private YhbTransactionService yhbTransactionService;
    @Autowired
    private YhbUserService yhbUserService;

    @Autowired
    private YhbActivityInfoService yhbActivityInfoService;
    @Autowired
    private YhbUserOrderService yhbUserOrderService;

    @Autowired
    private  YhbCurrentCountService yhbCurrentCountService;



    private void settlement() {
        log.info("----------东付解冻任务开始------");
        List<YhbUser> yhbUsers = yhbUserService.selectList(null);

        for (YhbUser yhbUser : yhbUsers) {
            List<YhbUserOrder> orders = yhbUserOrderService.selectList(new EntityWrapper<YhbUserOrder>().eq("user_id", yhbUser.getUserId())
                    .eq("activity_flag", 1)
                    .eq("pay_state", "success")
            );
            handleTerminal(orders, yhbUser);
        }
        log.info("----------东付解冻任务开始------");
    }

    private void handleTerminal(List<YhbUserOrder> orders, YhbUser yhbUser) {

        for (YhbUserOrder order : orders) {
            YhbActivityInfo yhbActivityInfo = yhbActivityInfoService.selectById(order.getGoodsId());

            if (yhbActivityInfo == null) {
                log.info("----------订单号"+order.getOrderNo()+"未查到------");
                continue;
            }

            YhbCurrentCount yhbCurrentCount = yhbCurrentCountService.selectOne(new EntityWrapper<YhbCurrentCount>().eq("relation_id",order.getSn()).eq("income_type",8));
            if(yhbCurrentCount!=null){
                log.info("----------机具号为"+order.getSn()+"金额已解冻,不进行第二次------");
                continue;
            }

            List<TranTransactionInfo> list = yhbTransactionService.selectList(
                    new EntityWrapper<TranTransactionInfo>()
                            .eq("sn", order.getSn())
                            .orderBy("transaction_date", true));
            if (list.size() > 0) {

                TranTransactionInfo t = list.get(0);

                Date beginDate = t.getTransactionDate();

                Date curDate = DateUtils.addDateMonths(beginDate, 3);

                if (curDate.compareTo(new Date()) != 1) {
                    List<Map<String, Object>> mapList = yhbTransactionService.queryTranAndCountList(order.getSn());
                    if (mapList.size() > 2) {
                        int count = 1;
                        for (Map<String, Object> map : mapList) {
                            Integer tranNum = Integer.valueOf(map.get("tranNum").toString());
                            if (tranNum >= 400) {
                                count++;
                            } else {
                                count = 1;
                            }
                            if (count == 3) {
                                BigDecimal amount = yhbActivityInfo.getPayAmount();
                                log.info("----------机具号为"+order.getSn()+"金额已解冻------");
                                yhbUserService.rebackFrozenAmount(yhbUser.getUserId(), amount,order.getSn());
                                break;
                            }

                        }
                    }
                }

            }
        }

    }
}
